package org.jiangpeng.customer.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.Properties;

/**
 * 属性文件读取类
 *
 * @author jiangpeng
 */
public final class PropsUtil {
    private static final Logger LOGGER = LoggerFactory.getLogger(PropsUtil.class);

    /**
     * 加载属性文件
     * @param fileName
     * @return
     */
    public  static Properties loadProps(String fileName){
        Properties props = null;
        InputStream is = null;
        try {
            is = Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName);
            if (is == null){
                throw new FileNotFoundException(fileName + " file is not found!");
            }
            props = new Properties();
            props.load(is);
        }
        catch (Exception e){
            LOGGER.error("load properties file failure,msp:" + e.getMessage(), e);
        }
        finally {
            if(is != null){
                try {
                    is.close();
                }
                catch (Exception e){
                    LOGGER.error("close input stream failure,msg:" + e.getMessage(), e);
                }
            }
        }
        return props;
    }

    /**
     * 获取字符串型属性
     * @param props
     * @param key
     * @param defaultValue
     * @return
     */
    public static  String getString(Properties props, String key, String defaultValue){
        String value = defaultValue;
        if (props.containsKey(key)){
            value = props.getProperty(key);
        }
        return value;
    }

    /**
     * 获取字符串型属性
     * @param props
     * @param key
     * @return
     */
    public static  String getString(Properties props, String key){
        return getString(props, key, "");
    }

    /**
     * 获取整形属性
     * @param properties
     * @param key
     * @param defaultValue
     * @return
     */
    public static int getInt(Properties properties, String key, int defaultValue){
        int intValue = defaultValue;
        if (properties.containsKey(key)){
            intValue = CastUtil.castInt(properties.getProperty(key));
        }
        return intValue;
    }

    /**
     * 获取整形属性
     * @param properties
     * @param key
     * @return
     */
    public static int getInt(Properties properties, String key){
        return getInt(properties, key, 0);
    }

    /**
     * 获取Boolean类型的属性
     * @param properties
     * @param key
     * @param defaultValue
     * @return
     */
    public static boolean getBoolean(Properties properties, String key, boolean defaultValue){
        boolean booleanValue = defaultValue;
        if (properties.containsKey(key)){
            booleanValue = CastUtil.castBoolean(properties.getProperty(key));
        }
        return booleanValue;
    }

    /**
     * 获取Boolean类型的属性
     * @param properties
     * @param key
     * @return
     */
    public static boolean getBoolean(Properties properties, String key){
        return getBoolean(properties, key, false);
    }
}
